﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class EmployeeManager
    {
        public EmployeeManager()
        { }
        private static EmployeeManager instance;
        public static EmployeeManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EmployeeManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, Employee> employeeCaches = new Dictionary<string, Employee>();
        public void LoadData()
        {
            employeeCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Employee>();
            IList<Employee> empList = null;
            empList = crt.List<Employee>();
            if (empList != null)
            {
                foreach (Employee emp in empList)
                {
                    employeeCaches.Add(emp.ID, emp);
                }
            }
        }
        /// <summary>
        /// 根据Id获取员工信息
        /// </summary>
        /// <param name="sId">ID</param>
        /// <returns></returns>
        public Employee GetEmployeeById(string sId)
        {
            if (employeeCaches.ContainsKey(sId))
            {
                return employeeCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有员工信息
        /// </summary>
        /// <returns></returns>
        public Employee[] GetEmployees()
        {
            return employeeCaches.Values.ToArray();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteEmployeeByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            Employee cmp = GetEmployeeById(sId);
            if (cmp != null)
            {
                session.Delete(cmp);
                employeeCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="productComment"></param>
        public void InsertEmployeeComment(Employee employee)
        {
            NHHelper.SaveOjb(employee);
            employeeCaches.Add(employee.ID, employee);
        }
    }
}
